package com.vsen.lpr.service;

import com.vsen.lpr.common.exception.BusinessException;
import com.vsen.lpr.common.model.Response;
import com.vsen.lpr.common.utils.BeanCopyUtil;
import com.vsen.lpr.common.utils.DateTimeUtil;
import com.vsen.lpr.common.utils.SnowflakeIdWorker;
import com.vsen.lpr.entity.*;
import com.vsen.lpr.mapper.*;
import com.vsen.lpr.pojo.dto.OrderListDto;
import com.vsen.lpr.pojo.dto.PlaceOrderDto;
import com.vsen.lpr.pojo.dto.PriceItemDto;
import com.vsen.lpr.pojo.vo.OrderDetailVo;
import com.vsen.lpr.pojo.vo.OrderListVo;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private BalanceMapper balanceMapper;


    @Transactional
    public Response placeOrder(PlaceOrderDto dto) {
        //核价
        checkOrder(dto);
        //雪花算法生成订单id
        SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(1, 12);
        String orderId = String.valueOf(snowflakeIdWorker.nextId());

        dto.setOrderId(orderId);
        dto.setShopId(1);
        Order order = new Order();
        BeanCopyUtil.copyFromSrcToDest(dto, order);
        //订单表插入
        orderMapper.insert(order);

        List<PriceItemDto> priceItem = dto.getPriceItem();
        //订单明细
        for (PriceItemDto p : priceItem) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNum(p.getNum());
            orderDetail.setProductId(p.getProductId());
            orderDetail.setName(p.getName());
            orderDetail.setPrice(p.getPrice());
            ProductInfo productInfo = productInfoMapper.selectById(p.getProductId());
            if (productInfo.getStock() < p.getNum()) {
                throw new BusinessException("30001", productInfo.getProductName() + "库存不够了！");
            }
            //订单明细表
            orderDetailMapper.insert(orderDetail);
            //减库存,加销量
            productInfoMapper.countStockAndAddSales(p.getProductId(), p.getNum());
            //删除购物车
            shoppingCartMapper.deleteById(p.getShoppingCartId());
        }

        Balance balance = balanceMapper.selectByUserId(dto.getUserId());
        if(balance ==null){
            throw new BusinessException("500002", "用户账户不存在");
        }
        if (balance.getBalance() < dto.getTotal()) {
            throw new BusinessException("30001", "爱心值不够了！请完成任务后获得爱心值");
        }
        //扣减余额
        balanceMapper.reduceBalance(dto.getUserId(),dto.getTotal());
        return Response.success();
    }


    private void checkOrder(PlaceOrderDto dto) {
        List<PriceItemDto> priceItem = dto.getPriceItem();
        for (PriceItemDto p : priceItem) {
            ShoppingCart shoppingCart = shoppingCartMapper.selectById(p.getShoppingCartId());
            //todo
        }
    }


    public Response<List<OrderListVo>> orderList(OrderListDto dto) {
        List<OrderListVo> orderListVo = new ArrayList<>();
        List<Order> orderList = orderMapper.selectUserOrderList(dto.getUserId(), dto.getShopId());

        for (Order o : orderList) {
            OrderListVo vo = new OrderListVo();
            List<OrderDetail> orderDetailDos = orderDetailMapper.selectByOrderId(o.getOrderId());
            List<OrderDetailVo> orderDetailVos = BeanCopyUtil.copyListFromSrcToDest(orderDetailDos, OrderDetailVo.class);
            vo.setOrderDetail(orderDetailVos);
            vo.setOrderTime(DateTimeUtil.format(o.getOrderTime(),"yyyy-MM-dd HH:mm:ss"));
            vo.setTotal(o.getTotal()/100);
            vo.setOrderId(o.getOrderId());
            vo.setStatus(o.getStatus());
            orderListVo.add(vo);
        }
        return Response.success(orderListVo);
    }
}
